package extensions;

import static util.ProjectUtil.*;
import TapestryModel.*;

public class Extensions {

    public static String getParamDeclarations(String str, Activation act) throws Throwable {
        try {
            if (act == null)
                return null;

            String retVal = "";

            for (ActivationMapping mapping : act.getParam())
                for (ContentData data : mapping.getData())
                    if (str.equals(data.getName()))
                        retVal += mapping.getType().toString() + " " + mapping.getName() + ",";

            if (retVal.equals(""))
                return retVal;

            return retVal.substring(0, retVal.length() - 1);

        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
    }

    public static String getParamNames(String str, Activation act) throws Throwable {
        try {
            if (act == null)
                return null;

            String retVal = "";

            for (ActivationMapping mapping : act.getParam())
                for (ContentData data : mapping.getData())
                    if (str.equals(data.getName()))
                        retVal += mapping.getName() + ",";

            return retVal.substring(0, retVal.length() - 1);

        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
    }

    public static String getActivationContextMethodBody(ParamLink link) {
        try {
            String out = "";

            ContentObject entity = link.getOwner();
            boolean list = entity.isList();
            Activation act = link.getParam().get(0).getMapping().getOwner();

            for (ActivationMapping mapping : act.getParam())
                for (LinkMapping linkMapping : link.getParam())
                    if (linkMapping.getMapping().getName().equals(mapping.getName()))
                        if (list)
                            out += entity.getName() + "LoopVariable.get" + firstUpper(linkMapping.getValue().getName())+ "(),";
                        else
                            out += entity.getName() + ".get" + firstUpper(linkMapping.getValue().getName()) + "(),";

            return out.substring(0, out.length() - 1);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    
    public static String getConstructorForType(String type){
        if(type.toLowerCase().equals("integer"))
            return "Integer.valueOf(0)";
        
        if(type.toLowerCase().equals("double"))
            return "new Double(0)";
        
        if(type.toLowerCase().equals("boolean"))
            return "Boolean.TRUE";
        
        return "new "+type+"()";
    }
}
